//
//  TopMenuView.m
//  小团购
//
//  Created by 樊荣海 on 15/1/23.
//  Copyright (c) 2015年 樊荣海. All rights reserved.
//

#import "TopMenuView.h"

@implementation TopMenuView {
    
    TopMenuBtn *_currentBtn;//临时按钮
    
    CategoryDropView *_cateView;
    DistrictDropView *_disView;
    OrderDropView *_orderView;
    
    DropMenuView *_currentDropView;
    
    NSMutableArray *_btnArray;
    
    NSArray *_titleArray;
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self == [super initWithFrame:frame]) {
        
        _btnArray = [NSMutableArray array];
        
        _titleArray = @[@"全部分类", @"全部商区", @"默认排序"];
        
        [self addTopMenuBtnAtIndex:0];
        [self addTopMenuBtnAtIndex:1];
        [self addTopMenuBtnAtIndex:2];
        
        [self initTitle];
        
        //监听
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initTitle) name:@"initTopTitle" object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeAllMenuView) name:@"removeAllMenuView" object:nil];
        
    }
    
    return self;
    
}

- (void)addTopMenuBtnAtIndex:(int)index {
    
    TopMenuBtn *btn = [[TopMenuBtn alloc] init];
    btn.frame = CGRectMake(MenuButtonW*index, 0, 0, 0);
    btn.tag = index;
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
    [self addSubview:btn];
    
    [_btnArray addObject:btn];
}

- (void)initTitle {
    
    for (TopMenuBtn *btn in _btnArray) {
        NSString *superTitle = [[DBTool sharedDBTool] selectSubTitle:(TabelType)btn.tag];
        if (superTitle.length>0) {
            btn.title = superTitle;
        } else {
            btn.title = _titleArray[btn.tag];
        }
        
    }
}

- (void)removeAllMenuView {
    
    if (_cateView) {
        [_cateView removeFromSuperview];
        _cateView = nil;
    }
    
    if (_disView) {
        [_disView removeFromSuperview];
        _disView = nil;
    }
    
    if (_orderView) {
        [_orderView removeFromSuperview];
        _orderView = nil;
    }
    
    _currentBtn.selected = NO;
    _currentBtn = nil;
}

- (void)btnClick:(TopMenuBtn *)sender {
    
    _currentBtn.selected = NO;
    
    if (_currentBtn == sender) {
        
        sender.selected = NO;
        _currentBtn = nil;
        
        //隐藏
        [self hideMenuView];
        
    } else{
        
        sender.selected = YES;
        _currentBtn = sender;
        
        //先移除当前
        [_currentDropView removeFromSuperview];
        
        //显示新的
        [self showMenuView:sender];
    }
    
}


//显示MenuView
- (void)showMenuView:(TopMenuBtn *)sender {
    
    BOOL animated = _currentDropView == nil;
    
    if (sender.tag == 0) {
        
        if (_cateView == nil) {
            _cateView = [[CategoryDropView alloc] init];
        }
        
        _currentDropView = _cateView;
        
    } else if (sender.tag == 1) {
        
        if (_disView == nil) {
            _disView = [[DistrictDropView alloc] init];
        }
        
        _currentDropView = _disView;
        
    } else if (sender.tag == 2) {
        
        if (_orderView == nil) {
            _orderView = [[OrderDropView alloc] init];
        }
        
        _currentDropView = _orderView;
        
    }
    
    _currentDropView.frame = self.contentView.bounds;
    
    __unsafe_unretained TopMenuView *weakSelf = self;
    _currentDropView.block = ^ {
        [weakSelf btnClick:weakSelf->_currentBtn];
    };
    
    [self.contentView addSubview:_currentDropView];
    
    //执行动画
    if (animated) {
        [_currentDropView showAnimation];
    }
    
}

//显示MenuView
- (void)hideMenuView {
    
    [_currentDropView hideAniamtion];
    _currentDropView = nil;
}





- (void)setFrame:(CGRect)frame {
    
    frame.size = CGSizeMake(3 * MenuButtonW, MenuButtonH);
    [super setFrame:frame];
}

- (void)setBounds:(CGRect)bounds {
    
    bounds.size = CGSizeMake(3 * MenuButtonW, MenuButtonH);
    [super setBounds:bounds];
}

@end
